import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutterclue/mvp/mode/login_mode.dart';
import 'dart:convert';

class SharePreferencesUtil {
  ///  int AgentId;
  //  int CityId;
  //  int DepartmentId;
  //  int RoleId;
  //  int TopAgentId;
  //  String Account;
  //  String Name;
  //  String RoleName;
  //  String SecretKey;
  //  String Token;
  //  List<FunctionCodeBean> FunctionCode;
  static const String RoleType="roletype";
  static const String TOKEN = "token";
  static const String AgentId = "AgentId";
  static const String CityId = "CityId";
  static const String DepartmentId = "DepartmentId";
  static const String RoleId = "RoleId";
  static const String TopAgentId = "TopAgentId";
  static const String Account = "Account";
  static const String Name = "Name";
  static const String RoleName = "RoleName";
  static const String SecretKey = "SecretKey";
  static const String FunctionCode = "FunctionCode";

  static Future saveInt(String name, int value) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.setInt(name, value);
  }

  static Future getInt(String name) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.getInt(name);
  }

  static Future saveString(String name, String value) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.setString(name, value);
  }

  static Future saveUserInfo(LoginBean bean) async {
    saveString(AgentId, bean.AgentId.toString());
    saveString(TopAgentId, bean.TopAgentId.toString());
    saveString(TOKEN, bean.Token.toString());
    saveInt(RoleType, bean.RoleType);
    saveString(CityId, bean.CityId.toString());
    saveString(DepartmentId, bean.DepartmentId.toString());
    saveString(RoleId, bean.RoleId.toString());
    saveString(Account, bean.Account.toString());
    saveString(Name, bean.Name.toString());
    saveString(RoleName, bean.RoleName.toString());
    saveString(SecretKey, bean.SecretKey.toString());
    saveString(FunctionCode, bean.FunctionCode.toString());
//    List list = json.decode(bean.FunctionCode.toString());
//    print("safasdfsdfsd"+list[0]["FunctionCode"]);
  }

  static Future clearUserInfo() async {
    saveString(AgentId, "");
    saveString(TopAgentId, "");
    saveString(TOKEN, "");
    saveInt(RoleType, 0);
    saveString(CityId, "");
    saveString(DepartmentId, "");
    saveString(RoleId, "");
    saveString(Account, "");
    saveString(Name, "");
    saveString(RoleName, "");
    saveString(SecretKey, "");
    saveString(FunctionCode, "");
  }

  static Future getString(String name) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.getString(name);
  }

  static Future saveBool(String name, bool value) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.setBool(name, value);
  }

  static Future getBool(String name) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.getBool(name);
  }

  static Future saveDouble(String name, double value) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.setDouble(name, value);
  }

  static Future getDouble(String name) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.getDouble(name);
  }

  static Future saveList(String name, List list) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.setStringList(name, list);
  }

  static Future getStringList(String name) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.getDouble(name);
  }
}
